package com.xnyzc.lhy.order.service.impl.sidebar;

import com.alibaba.fastjson.JSON;
import com.xnyzc.lhy.common.component.redis.RedisCommon;
import com.xnyzc.lhy.common.constant.RedisPrefixConstant;
import com.xnyzc.lhy.common.penum.vehicle.EvehicleType;
import com.xnyzc.lhy.common.util.CheckUtil;
import com.xnyzc.lhy.order.entity.gps.OaDVehicleGps;
import com.xnyzc.lhy.order.feign.amap.IAmapService;
import com.xnyzc.lhy.order.service.sidebar.IOaCNearbyDriverPositionService;
import com.xnyzc.lhy.resource.entity.amap.AmapGouldResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @Description
 * @Author Bin
 * @Date 2019/8/16 13:41
 **/
@Service
@Slf4j
public class OaCNearbyDriverPositionServiceImpl implements IOaCNearbyDriverPositionService {

    @Autowired
    private RedisCommon redisCommon;

    @Autowired
    IAmapService amapService;

    /**
     * 获取附近车辆信息
     *
     * @param lngClient 经度
     * @param latClient 纬度
     */
    @Override
    public Map<String, Object> selectNearbyDriverPosition(String lngClient, String latClient) {
        Map<String, Object> result = new HashMap<>(16);
        String startPoint = new StringBuffer(lngClient).append(",").append(latClient).toString();
        // 全部在线司机
        Set<String> keys = redisCommon.keys(String.format("%s*", RedisPrefixConstant.GPS_VEH_KEY_PREFIX));
        List<Map<String, Object>> list = new ArrayList<>();
        for (String key : keys) {
            // 获取司机jsonString
            String jsonStr = redisCommon.getStr(key);
            if (CheckUtil.objIsEmpty(jsonStr)) {
                continue;
            }
            OaDVehicleGps oaDVehicleGps = JSON.parseObject(jsonStr, OaDVehicleGps.class);
            if (CheckUtil.objIsEmpty(oaDVehicleGps)) {
                continue;
            }
            // 获取司机经纬度
            String lng = oaDVehicleGps.getLng();
            String lat = oaDVehicleGps.getLat();
            if (CheckUtil.objIsEmpty(lat, lng)) {
                continue;
            }
            String endPoint = new StringBuffer(lng).append(",").append(lat).toString();
            // 获取距离
            AmapGouldResult distance = amapService.getDistancePoint(startPoint, endPoint);
            if (CheckUtil.objIsEmpty(distance)) {
                continue;
            }
            List<AmapGouldResult.Results> distanceResults = distance.getResults();
            if (distanceResults.size() <= 0) {
                continue;
            }
            String distanceStr = distanceResults.get(0).getDistance();
            if (CheckUtil.objIsEmpty(distanceStr)) {
                continue;
            }
            // 判断距离
//            if (new BigDecimal(distanceStr).compareTo(new BigDecimal("1000")) <= 0){
            Map<String, Object> map = new HashMap<>(16);
            map.put("lng", lng);
            map.put("lat", lat);
            log.info("记录轨迹上传日志 --->>> lng:" + lng + " lat:" + lat);
            map.put("vehType", oaDVehicleGps.getVehType());
            map.put("vehTypeName", EvehicleType.findById(oaDVehicleGps.getVehType()).getValue());
            map.put("vehImage", "");
            list.add(map);
//            }
        }
        result.put("list", list);
        return result;
    }
}
